package com.web.surf.sample.eshop.servlets;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.web.surf.sample.eshop.database.manager.interfaces.IImagesService;
import com.web.surf.sample.eshop.database.manager.services.ImagesServiceImpl;
import com.web.surf.sample.eshop.faces.managed.beans.ImageDB;

/**
 * The Image servlet for serving from database.
 */
public class ImageServlet extends HttpServlet {

  	private static final long serialVersionUID = 7314165160149247817L;
	private static final int DEFAULT_BUFFER_SIZE = 20480; //20KB.
    private  IImagesService imagesService=new ImagesServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        // Get ID from request.
    	String sgoodsId = request.getParameter("id");

        // Check if ID is supplied to the request.
        if (sgoodsId == null) {            
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }
        Integer goodsId = Integer.parseInt(request.getParameter("id"));
        // Lookup Image by ImageId in database.
        ImageDB image = imagesService.getImageByGoodsID(goodsId);

        // Check if image is actually retrieved from database.
        if (image == null) {
            // Do your thing if the image does not exist in database.
            // Throw an exception, or send 404, or show default/warning image, or just ignore it.
            response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
            return;
        }

        // Init servlet response.
        response.reset();
        response.setBufferSize(DEFAULT_BUFFER_SIZE);
   //     response.setContentType(image.getContentType());
    //    response.setHeader("Content-Length", String.valueOf(image.getLength()));
    //    response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");

        // Prepare streams.
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            // Open streams.
            input = new BufferedInputStream(image.getImageBytes(), DEFAULT_BUFFER_SIZE);
            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

            // Write file contents to response.
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
        } finally {
            close(output);
            close(input);
        }
    }
  
    private static void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        }
    }
    
    //Spring
	public void setImagesService(IImagesService imagesService) {
		this.imagesService = imagesService;
	}

}